package com.rran.study.concurrent.safeend;

/**
 * @author yy
 * @Type EndThread.java
 * @Desc 阻塞方法中抛出InterruptedException异常后，如果需要继续中断，需要手动再中断一次
 * @date 2020/7/29 11:46
 */
public class HasInterrputException {
	
	private static class UseThread extends Thread{
		
		public UseThread(String name) {
			super(name);
		}
		
		@Override
		public void run() {
			while(!isInterrupted()) {
				//而sleep()和wait()方法内部会不断地轮询检查InterruptStatus状态值，如果某一时刻变为false的时候，
				// 当前线程就会中断阻塞状态，通过抛出InterrupException的方式来中断阻塞状态，然后继续执行下去。
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					System.out.println(Thread.currentThread().getName()
							+" in InterruptedException interrupt flag is "
							+isInterrupted());
					//资源释放
					interrupt();
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()
						+ " I am extends Thread.");
			}
			System.out.println(Thread.currentThread().getName()
					+" interrupt flag is "+isInterrupted());
		}
	}

	public static void main(String[] args) throws InterruptedException {
		Thread endThread = new UseThread("HasInterrputEx");
		endThread.start();
		Thread.sleep(500);
		endThread.interrupt();
		

	}

}
